%% Generate signal squence of source 1:
N = 256;
f = (0.001:0.001:0.5);
Sf_mean = zeros(1,500);
for k = 1:N
    xn = XnGen(N);
    Sf = Period(xn,f,N);
    Sf_mean = Sf_mean + Sf;
    hold on;
    plot(f,Sf);
end
Sf_mean = Sf_mean/N;
figure;

plot(f,Sf_mean);

%% using function library provided by matlab
%% Generate signal squence of source 1:
N = 256;
A2 = [-1.352 1.338 -0.662 0.240];
A3 = [-2.760 3.809 -2.654 0.924];
delta = 1;
for k = 1:50
%      xn = XnGen(N);
    xn = AR_Gen(A3,delta,N);
    [Sf_period w1] = periodogram(xn);
    [Sf_pcorr w2] = pyulear(xn,4);
    [Sf_pcov w3] = pcov(xn,20);
    [Sf_pmcov w4] = pmcov(xn,4);
    [Sf_pburg w5] = pburg(xn,4);
    if(k == 1)
        Sf_period_sum = Sf_period;
        Sf_pcorr_sum = Sf_pcorr;
        Sf_pcov_sum = Sf_pcov;
        Sf_pmcov_sum = Sf_pmcov;
        Sf_pburg_sum = Sf_pburg;
    else
        Sf_period_sum = Sf_period_sum + Sf_period;
        Sf_pcorr_sum = Sf_pcorr_sum + Sf_pcorr;
        Sf_pcov_sum = Sf_pcov_sum + Sf_pcov;
        Sf_pmcov_sum = Sf_pmcov_sum + Sf_pmcov;
        Sf_pburg_sum = Sf_pburg_sum + Sf_pburg;
    end
    subplot(5,2,1);
    hold on; plot(w1,pow2db(Sf_period));
    subplot(5,2,3);
    hold on; plot(w2,pow2db(Sf_pcorr));
    subplot(5,2,5);
    hold on; plot(w3,pow2db(Sf_pcov));
    subplot(5,2,7);
    hold on; plot(w4,pow2db(Sf_pmcov));
    subplot(5,2,9);
    hold on; plot(w5,pow2db(Sf_pburg));
end
Sf_period_mean = Sf_period_sum/N;
Sf_pcorr_mean = Sf_pcorr_sum/N;
Sf_pcov_mean = Sf_pcov_sum/N;
Sf_pmcov_mean = Sf_pmcov_sum/N;
Sf_pburg_mean = Sf_pburg_sum/N;

subplot(5,2,2);
hold on; plot(w1,pow2db(Sf_period_mean));
subplot(5,2,4);
hold on; plot(w2,pow2db(Sf_pcorr_mean));
subplot(5,2,6);
hold on; plot(w3,pow2db(Sf_pcov_mean));
subplot(5,2,8);
hold on; plot(w4,pow2db(Sf_pmcov_mean));
subplot(5,2,10);
hold on; plot(w5,pow2db(Sf_pburg_mean));

